package com.stox.client.ui.javafx.navigator.view;

import java.util.List;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import com.stox.client.api.core.Client;
import com.stox.client.ui.core.model.SearchableDecorator;
import com.stox.client.ui.javafx.core.StoxPlatform;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.client.ui.javafx.navigator.widget.NavigableScripListView;
import com.stox.client.ui.javafx.navigator.widget.NavigatorConfigurationPane;
import com.stox.client.ui.javafx.widget.ConfigurationPopupControl;
import com.stox.client.ui.javafx.widget.SearchPanel;
import com.stox.client.ui.javafx.workbench.stage.StoxStage;
import com.stox.client.ui.javafx.workbench.view.View;
import com.stox.shared.core.i18n.MessageSource;
import com.stox.shared.core.model.Scrip;
import com.stox.shared.core.model.filter.ScripCriteria;

public class NavigatorView extends View {
	
	private final ScripCriteria scripCriteria = new ScripCriteria();
	private NavigableScripListView navigableScripListView = new NavigableScripListView();
	private SearchPanel<Scrip> searchPanel = new SearchPanel<>(new SearchableDecorator<Scrip>(navigableScripListView.getNavigable()));
	private NavigatorConfigurationPane navigatorConfigurationPane;
	
	public NavigatorView() {
		super(MessageSource.get("Navigator"));
		UI.style(this, CSS.navigatorView);
		
		addScripSelectionEventHandlers();
		
		setCenter(navigableScripListView);
		setBottom(searchPanel);
		
		setSize();
		setLocation();
		
		updateData();
	}

	private void addScripSelectionEventHandlers(){
		searchPanel.getTextField().addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>(){
			@Override
			public void handle(KeyEvent event) {
				if(KeyCode.ENTER.equals(event.getCode())){
					navigableScripListView.publishEvent();
				}
			}
		});
	}
	
	@Override
	public void configure() {
		super.configure();
		if(null == navigatorConfigurationPane){
			navigatorConfigurationPane = new NavigatorConfigurationPane(navigableScripListView);
		}
		ConfigurationPopupControl configurationPopupControl = new ConfigurationPopupControl(navigatorConfigurationPane);
		configurationPopupControl.getApplyButton().addEventFilter(ActionEvent.ACTION, new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent arg0) {
				navigatorConfigurationPane.getScripCriteriaPanel().value(scripCriteria);
				updateData();
			}
		});
		configurationPopupControl.show(StoxStage.INSTANCE);
	}
	
	private void updateData(){
		new Thread(){
			public void run() {
				final List<Scrip> scrips = Client.SCRIP.find(scripCriteria);
				StoxPlatform.runLater(new Runnable(){
					@Override
					public void run() {
						navigableScripListView.getItems().setAll(scrips);
					}
				});
			};
		}.start();
	}
	
	public NavigableScripListView getNavigableScripListView() {
		return navigableScripListView;
	}

	private void setSize(){
		setPrefHeight(StoxStage.INSTANCE.getContentPane().getHeight());
		setPrefWidth(StoxStage.INSTANCE.getContentPane().getWidth()/5);
	}
	
	private void setLocation(){
		Bounds bounds = StoxStage.INSTANCE.getContentPane().getLayoutBounds();
		setLayoutX(bounds.getMinX());
		setLayoutY(bounds.getMinY());
	}
	
}